<?php


namespace app\admin\controller;


use think\facade\Filesystem;

class Image extends AdminBase
{
    public function upload() {
        if (!$this->request->isPost()) {
            return show(config('status.error'), '请求不合法');
        }
        //获取文件信息
        $file = $this->request->file('file');
        // 1.判断文件类型  2.文件大小限制600kb

        //上传图片
        $filePath = Filesystem::disk('public')->putFile('image', $file);
        if (!$filePath) {
            return show(config('status.error'), '图片上传失败');
        }
        $imageUrl = ['image' => '/upload/' . $filePath];
        return show(config('status.success'), '图片上传成功', $imageUrl);
    }

    public function layUpload() {
        if(!$this->request->isPost()) {
            return show(config("status.error"), "请求不合法");
        }
        $file = $this->request->file("file");
        $filename = \think\facade\Filesystem::disk('public')->putFile("image", $file);
        if(!$filename) {
            return json(["code" => 1, "data" => []], 200);
        }

        $result = [
            "code" => 0,
            "data" => [
                "src" => "/upload/".$filename,
            ],
        ];
        return json($result, 200);
    }
}